package com.hwadee.job.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hwadee.job.entity.Resume;
import com.hwadee.job.entity.User;
import com.hwadee.job.service.ResumeService;
import com.hwadee.job.util.Utils;

@RequestMapping("/resume")
@Controller
public class ResumeController {
	@Autowired
	private ResumeService resumeService;
	
	
	@RequestMapping("/UpdateUserInfo")
	public String addUser(Resume resume,MultipartFile file,HttpServletRequest request) throws IOException {
		
		resumeService.insert(resume);
		return "student/index";
	}
	
	
	@GetMapping("/check")
	public String check(Model model,HttpServletRequest request,HttpSession session) {
		User user=(User) session.getAttribute("user");
		Resume re=resumeService.selectByName(user.getuName());
//		System.err.println(re.toString());
		model.addAttribute("resume", re);
		return "student/check";
	}
	@GetMapping("/update")
	public String update(Model model,HttpServletRequest request,HttpSession session) {
		User user=(User) session.getAttribute("user");
		Resume re=resumeService.selectByName(user.getuName());
		System.err.println(re.toString());
		model.addAttribute("resume", re);
		return "student/updateintro";
	}
	
//	
//	@PostMapping("/update1")
//	public String update1(Model model,HttpServletRequest request,Resume resume,MultipartFile file) {
//		
//		System.out.println("修改提交的用户："+resume);
//		String filePath = request.getSession().getServletContext().getRealPath("/resources/images");; //定义图片上传后的路径
//		String newFileName = fileOperate(file,filePath);
//		resume.setrHeadPort(newFileName);
//		System.out.println("修改最后的user:"+resume);
//		resumeService.updateByPrimaryKey(resume);
//		return "redirect:/resume/check";
//	}
//	
//	
	
	/**
	 * 封装操作文件方法， 添加用户 和修改用户都会用到
	 * @param file
	 * @param filePath
	 * @return
	 */
//	private String fileOperate(MultipartFile file,String filePath) {
//		String originalFileName = file.getOriginalFilename();//获取原始图片的扩展名
//		System.out.println("图片原始名称："+originalFileName);
//		String newFileName = UUID.randomUUID()+originalFileName;  //新的文件名称
//		System.out.println("新的文件名称："+newFileName);
//		File targetFile = new File(filePath,newFileName); //创建新文件
//		try {
//			file.transferTo(targetFile); //把本地文件上传到文件位置 , transferTo()是springmvc封装的方法，用于图片上传时，把内存中图片写入磁盘
//		} catch (IllegalStateException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}   
//		return newFileName;
//	}
	
	
	/**
     * Ajax文件上传，上传用户头像并返回上传后的路径文件名
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/HeadPortUpload.do")
    @ResponseBody
    public String HeadPortUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) {
        String fileName = Utils.getNewFileName(file.getOriginalFilename()); //获取一个新的文件名
        String filePath_1 = "/resources/images/";     //文件上传路径
        String filePath_2 = request.getSession().getServletContext().getRealPath("/"); //项目路径
        try {
            //实例化文件路径
            File filePath = new File(filePath_2 + filePath_1);
            if (!filePath.exists()) {     //检查文件路径是否存在
                filePath.mkdirs();      //不存在则创建目录
            }
            File newFile = new File(filePath, fileName);
            file.transferTo(newFile);//通过CommonsMultipartFile的方法直接写文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return filePath_1 + fileName;
    }
    /**
     * 修改用户信息
     * @param userDetailed
     * @param model   http://127.0.0.1:8080/resources/images/2a05c5081df842b6be225f94f638c2b2.JPG
     * @return		  http://127.0.0.1:8080/resources/images/8e69d294-2644-4098-8ec5-861202aeb64d636434212176945680.png
     */
    @RequestMapping(value = "/UpdateUserInfo.do")
    public String UpdateUserInfo(Resume resume, HttpServletRequest request, Model model) {
        System.out.println(resume);
        Resume ud = resumeService.selectByPrimaryKey(resume.getrId());
        String oldHeadPort = ud.getrHeadPort();
        if (oldHeadPort != null && oldHeadPort != "" && !oldHeadPort.equals(resume.getrHeadPort())) {
            String filePath = request.getSession().getServletContext().getRealPath("/");
            File file = new File(filePath + oldHeadPort);
            if (file.exists()) {     //检查文件是否存在
                file.delete();      //存在则删除文件
            }
        }
        
        resumeService.updateByPrimaryKey(resume);
        //返回用户详细信息
        return "redirect:/resume/check";
    }
	
    
    
    
}
